package org.adorsys.aderp.jpa.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.BeanFactory;

public class ToOneRelationshipInfoBuilder {
	
	public static List<ToOneRelationshipInfo> build(
			Class<?> klass, BeanFactory beanFactory){
		List<Field> fields = FieldResolver.extractField(klass, new ManyToOneAnnotationFilter(), new OneToOneAnnotationFilter());
		List<ToOneRelationshipInfo> result = new ArrayList<ToOneRelationshipInfo>();
		for (Field field : fields) {
			result.add(processAnnotatedField(klass, field,beanFactory));
		}
		return result;
	}	
	
	private static ToOneRelationshipInfo processAnnotatedField(Class<?> klass, 
			Field field, BeanFactory beanFactory)
	{
		Method getterMethod = FieldResolver.getterMethod(field, klass);
		Method setterMethod = FieldResolver.setterMethod(field, klass);
		return new ToOneRelationshipInfo(field, getterMethod, setterMethod, beanFactory);
	}
}
